package com.narbeshuber.tablet.lasalara.fragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.narbeshuber.tablet.lasalara.LasaLaraActivity;
import com.narbeshuber.tablet.lasalara.R;
import com.narbeshuber.tablet.lasalara.Utils;
import com.narbeshuber.tablet.lasalara.fragment.InfoFragment.LayoutType;
import com.narbeshuber.tablet.lasalara.listadapters.QuestionListAdapter;

public class QuestionListFragment extends ListFragment {
	
	private LasaLaraActivity lasalaraActivity;
	
	public QuestionListFragment(LasaLaraActivity lasalaraActivity){
		this.lasalaraActivity = lasalaraActivity;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.questionlistlayout, container, false);
		setHasOptionsMenu(true);
		return v;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		calculateProgress();
	}
	
	public QuestionListAdapter getListAdapter(){
		return (QuestionListAdapter) super.getListAdapter();
	}
	
	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		
		MenuItem item = menu.findItem(R.id.pageview);
		item.setEnabled(true);
		item.setVisible(true);
		item.setIcon(R.drawable.ic_menu_pageview_active);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			public boolean onMenuItemClick(MenuItem item) {
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					lasalaraActivity.hideInfo();
				}
				FragmentManager fragmentManager = (FragmentManager) getActivity()
				.getFragmentManager();
				FragmentTransaction fragmentTransaction = fragmentManager
						.beginTransaction();
				QuestionFragment questionFragment = new QuestionFragment(lasalaraActivity, 
						lasalaraActivity.getDbHelper().getAllQuestionsFromChapter(ChapterFragment.chapter.getChapterKey()));
				fragmentTransaction
						.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
				fragmentTransaction.replace(R.id.right, questionFragment);
				fragmentTransaction.commit();
				LasaLaraActivity.pageView = false;
				return true;
			}
			
		});
		item = menu.findItem(R.id.night_mode);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					lasalaraActivity.hideInfo();
				}
				LasaLaraActivity.ninjaMode = !LasaLaraActivity.ninjaMode;
				if(LasaLaraActivity.ninjaMode){
					lasalaraActivity.setNightMode();
					getListAdapter().notifyDataSetChanged();
					item.setIcon(R.drawable.ic_menu_nightmode_active);
				} else {
					lasalaraActivity.setDayMode();
					getListAdapter().notifyDataSetChanged();
					item.setIcon(R.drawable.ic_menu_nightmode);
				}
				ChapterFragment.getInstance().getListAdapter().notifyDataSetChanged();
				return true;
			}
		});
		item = menu.findItem(R.id.text_size_down);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					lasalaraActivity.hideInfo();
				}
				Utils.decreaseTextSize();
				getListAdapter().notifyDataSetChanged();
				return true;
			}
		});
		item.setEnabled(true);
		item.setVisible(true);
		item = menu.findItem(R.id.text_size_up);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					lasalaraActivity.hideInfo();
				}
				Utils.increaseTextSize();
				getListAdapter().notifyDataSetChanged();
				return true;
			}
		});
		item = menu.findItem(R.id.switch_qa);
		item.setEnabled(true);
		item.setVisible(true);
		if (LasaLaraActivity.reverseMode) {
			item.setIcon(R.drawable.ic_menu_switch_active);
		}
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					lasalaraActivity.hideInfo();
				}
				LasaLaraActivity.reverseMode = !LasaLaraActivity.reverseMode;
				getListAdapter().notifyDataSetChanged();
				if (LasaLaraActivity.reverseMode) {
					item.setIcon(R.drawable.ic_menu_switch_active);
				} else {
					item.setIcon(R.drawable.ic_menu_switch);
				}
				return true;
			}
		});
		item = menu.findItem(R.id.info);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
		
			public boolean onMenuItemClick(MenuItem item) {
				FragmentManager fragmentManager = getFragmentManager();
		        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
				if (getActivity().findViewById(R.id.right_top).getVisibility() == View.GONE){
			        fragmentTransaction.replace(R.id.right_top, new InfoFragment(LayoutType.STUDY));
		        	fragmentTransaction.commit();
		        	getActivity().findViewById(R.id.right).setVisibility(View.GONE);
		        	getActivity().findViewById(R.id.right_top).setVisibility(View.VISIBLE);
		        	item.setIcon(R.drawable.ic_menu_info_active);
		        	return true;
				} else {
					fragmentTransaction.remove(fragmentManager.findFragmentById(R.id.right_top));
		        	fragmentTransaction.commit();
		        	getActivity().findViewById(R.id.right).setVisibility(View.VISIBLE);
		        	getActivity().findViewById(R.id.right_top).setVisibility(View.GONE);
		        	item.setIcon(R.drawable.ic_menu_info);
					return true;
				}
			}
		
		});
	}
	
	public void calculateProgress(){
		int totalQuestions = ChapterFragment.chapter.getTotalQuestions();
		int answeredQuestions = ChapterFragment.chapter.getAnswered();
		ProgressBar progressBar = (ProgressBar)getActivity().findViewById(R.id.progressChapter);
		int ansPercentage = (int)Math.round((double)answeredQuestions/(double)totalQuestions*100);
		progressBar.setProgress(ansPercentage);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		TextView answer = (TextView)v.findViewById(R.id.answer);
		if (answer.getVisibility()==View.VISIBLE){
			answer.setVisibility(View.GONE);
			v.findViewById(R.id.anwserLine).setVisibility(View.GONE);
			v.findViewById(R.id.anwserLayoutList).setVisibility(View.GONE);
		} else {
			for (int i=0; i < l.getChildCount(); i++){
				View childView = l.getChildAt(i);
				TextView childAnswer = (TextView)childView.findViewById(R.id.answer);
				childAnswer.setVisibility(View.GONE);
			}
			answer.setVisibility(View.VISIBLE);
			v.findViewById(R.id.anwserLine).setVisibility(View.VISIBLE);
			v.findViewById(R.id.anwserLayoutList).setVisibility(View.VISIBLE);
		}
	}

}
